﻿using BielskoNoca.DAL.Abstract;
using BielskoNoca.DAL.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Web;

namespace BielskoNoca.DAL.Concrete
{

    public class ContestRepository : IContestRepository
    {
        private BielskoNocaDataEntities context;
            //= new BielskoNocaDataEntities();

        public ContestRepository(BielskoNocaDataEntities context)
        {
            this.context = context;
        }

        public IQueryable<Contest> Query
        {
            get { return this.context.Contests; }
        }

        public int AddRunner(Contest contest, RunnerInfo info, Runner runner)
        {
            Runner addedRunner = context.Runners.Add(runner);
            //RunnerInfo addedRunnerInfo = context.RunnerInfoes.Add(info);
            context.SaveChanges();

            return addedRunner.Id;
        }

        public int Add(Contest entity)
        {
            Contest tEntity = this.context.Contests.Add(entity);
            this.context.SaveChanges();

            return tEntity.Id;
        }

        public Contest GetFirstOrDefault(Expression<Func<Contest, bool>> whereCondition)
        {
            return this.context.Contests.FirstOrDefault(whereCondition);
        }

        public void Delete(Contest entity)
        {
            throw new NotImplementedException();
        }

        public void Attach(Contest entity)
        {
            throw new NotImplementedException();
        }

        public IList<Contest> GetAll(Expression<Func<Contest, bool>> whereCondition)
        {
            throw new NotImplementedException();
        }

        public IList<Contest> GetAll()
        {
            return this.Query.ToList();
        }

        public long Count(System.Linq.Expressions.Expression<Func<Contest, bool>> whereCondition)
        {
            throw new NotImplementedException();
        }

        public long Count()
        {
            throw new NotImplementedException();
        }

        public Contest GetById(int id)
        {
            return this.GetFirstOrDefault(con => con.Id == id);
        }

        public Contest GetByUrl(string url)
        {
            return this.GetFirstOrDefault(con => con.UrlName == url);
        }

        public Category AddCategory(Contest contest, Category category)
        {
            Category tEntity = context.Categories.Add(category);
            context.SaveChanges();

            return tEntity;
        }

        public IList<Category> GetCategories(int contestId)
        {
            return this.context.Categories.Where(cat => cat.ContestId == contestId).ToList();
        }

        public Category GetCategoryById(int contestId, int categoryId)
        {
            return this.context.Categories.FirstOrDefault(cat => cat.ContestId == contestId && cat.Id == categoryId);
        }
    }
}